<?php

/**
 * 商户管理
 * @author Administrator
 */

namespace Operation\Controller;

use Think\Controller;
use Think\IReq;
use Think\IFilter;
use Think\IFile;
use Think\Page;
use Think\JSON;
use Think\ITime;

class PartnerController extends ComController {

    public function seller_list() {
        $p = IFilter::act(IReq::get('p'), 'int') ? IFilter::act(IReq::get('p'), 'int') : 1;
        $page = '20';
        $where = array(
            'is_del' => 0
        );
        $order = 'id desc';
        $partnerObj = M('seller');
        $partner = $partnerObj->where($where)->order($order)->page($p . ",$page")->select();
        $this->assign('partner', $partner);
        $this->display();
    }

    public function seller_edit() {
        $seller_id = IFilter::act(IReq::get('id'));
        $sellerObj = M('seller');
        $sellerRow = $sellerObj->where(array('id' => $seller_id))->find();
        //dump($sellerRow);die;
        $this->assign('sellerRow', $sellerRow);
        $this->assign('json_sellerRow', JSON::encode($sellerRow));
        $this->display();
    }

    /**
     * @brief 商户的增加动作
     */
    public function seller_add() {
        $seller_id = IFilter::act(IReq::get('id'), 'int');
        $seller_name = IFilter::act(IReq::get('seller_name'));
        $email = IFilter::act(IReq::get('email'));
        $password = IFilter::act(IReq::get('password'));
        $repassword = IFilter::act(IReq::get('repassword'));
        $truename = IFilter::act(IReq::get('true_name'));
        $phone = IFilter::act(IReq::get('phone'));
        $mobile = IFilter::act(IReq::get('mobile'));
        $province = IFilter::act(IReq::get('province'), 'int');
        $city = IFilter::act(IReq::get('city'), 'int');
        $area = IFilter::act(IReq::get('area'), 'int');
        $cash = IFilter::act(IReq::get('cash'), 'float');
        $is_vip = IFilter::act(IReq::get('is_vip'), 'int');
        $is_lock = IFilter::act(IReq::get('is_lock'), 'int');
        $address = IFilter::act(IReq::get('address'));
        $account = IFilter::act(IReq::get('account'));
        $server_num = IFilter::act(IReq::get('server_num'));
        $home_url = IFilter::act(IReq::get('home_url'));
        $sort = IFilter::act(IReq::get('sort'), 'int');
        $name = IFilter::act(IReq::get('name'));

        if (!$seller_id && $password == '') {
            $errorMsg = '请输入密码！';
        }

        if ($password != $repassword) {
            $errorMsg = '两次输入的密码不一致！';
        }

        //创建商家操作类
        $sellerDB = M("seller");

        if ($sellerDB->where(array('seller_name' => $seller_name, "id != {$seller_id}"))->find()) {
            $errorMsg = "登录用户名重复";
        } else if ($sellerDB->where(array('true_name' => $truename, "id != {$seller_id}"))->find()) {
            $errorMsg = "商户真实全程重复";
        }

        //操作失败表单回填
        if (isset($errorMsg)) {
            $this->sellerRow = $_POST;
            $this->redirect('seller_edit', false);
            Util::showMessage($errorMsg);
        }

        //待更新的数据
        $sellerRow = array(
            'true_name' => $truename,
            'account' => $account,
            'phone' => $phone,
            'mobile' => $mobile,
            'email' => $email,
            'address' => $address,
            'is_vip' => $is_vip,
            'is_lock' => $is_lock,
            'cash' => $cash,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'server_num' => $server_num,
            'home_url' => $home_url,
            'sort' => $sort,
            'name' =>$name
        );

        //商户资质上传
        if (isset($_FILES['paper_img']['name']) && $_FILES['paper_img']['name']) {
            $uploadObj = new PhotoUpload();
            $uploadObj->setIterance(false);
            $photoInfo = $uploadObj->run();
            if (isset($photoInfo['paper_img']['img']) && file_exists($photoInfo['paper_img']['img'])) {
                $sellerRow['paper_img'] = $photoInfo['paper_img']['img'];
            }
        }

        //添加新会员
        if (!$seller_id) {
            $sellerRow['seller_name'] = $seller_name;
            $sellerRow['password'] = md5($password);
            $sellerRow['create_time'] = ITime::getDateTime();

            $sellerDB->add($sellerRow);
        }
        //编辑会员
        else {
            //修改密码
            if ($password) {
                $sellerRow['password'] = md5($password);
            }

            $sellerRow['id'] = $seller_id;
            $sellerDB->save($sellerRow);
        }
        $this->redirect('seller_list');
    }

    /**
     * @brief 商户的删除动作
     */
    public function seller_del() {
        $id = IFilter::act(IReq::get('id'), 'int');
        $sellerDB = new IModel('seller');
        $data = array('is_del' => 1);
        $sellerDB->setData($data);

        if (is_array($id)) {
            $sellerDB->update('id in (' . join(",", $id) . ')');
        } else {
            $sellerDB->update('id = ' . $id);
        }
        $this->redirect('seller_list');
    }

}

